package cn.edu.zust.se.shop.service.impl;

import cn.edu.zust.se.shop.dao.OrderRepository;
import cn.edu.zust.se.shop.domain.Order;
import cn.edu.zust.se.shop.dto.OrderDto;
import cn.edu.zust.se.shop.dto.ProductDto;
import cn.edu.zust.se.shop.dto.UserDto;
import cn.edu.zust.se.shop.service.OrderService;
import cn.edu.zust.se.shop.service.ProductService;
import cn.edu.zust.se.shop.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderRepository orderDao;
    @Autowired
    ProductService productService;
    @Autowired
    UserService userService;
    @Override
    public OrderDto addOrder(Integer uid, Integer pid, int num) {
        Order order=new Order();
        order.setPid(pid);
        order.setUid(uid);
        ProductDto dto=productService.getProductById(pid);
        if(dto==null)
            return null;
        UserDto userDto=userService.getUserById(uid);
        order.setPname(dto.getPname());
        order.setPprice(dto.getPprice());
        order.setNumber(num);
        order.setUsername(userDto.getUsername());
        orderDao.save(order);
        OrderDto orderDto=new OrderDto();
        BeanUtils.copyProperties(order,orderDto);
        return orderDto;
    }
}
